Delete orphaned media in clean command #3419
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The use-case here is to automatically delete orphaned media objects, which also deletes them from the disk.
In database structure, it's common to use cascading deletes via foreign keys. When deleting database records like that, the
Media
object isn't automatically deleted due to its morphed relation. This delete action should be implemented by the developer to delete the related media objects in thedeleting
event, but this can be easily missed. No direct effects are visible on application level, but deleted storage objects remain indefinitely in themedias
table, as well as on storage, which might infringe on GDPR requirements, as well as unnessesary storage used.This PR adds logic to automatically delete orphaned models in the clean command.
I've added it as an opt-out, but if a safer opt-in option (I've added it as an opt-in option, where--delete-orphaned
instead of--skip-orphaned
)--delete-orphaned
needs to be included to delete orphaned models.Happy to chat about other approaches if preferred :)